#include <iostream>
#include <sys/poll.h>
#include <unistd.h>
#include <cstring>

int main()
{
    struct pollfd pfd;
    pfd.fd = 0;
    pfd.events = POLLIN;
    pfd.revents = 0;
    //   -1 阻塞
    //    0 非阻塞
    // time time之前是阻塞,time之后是非阻塞
    int timeout = -1;
    for (;;)
    {
        int ret = poll(&pfd, 1, -1);
        switch (ret)
        {
        case -1:
            std::cerr << "poll errno" << std::endl;
            break;
        case 0:
            std::cout << "time out" << std::endl;
            break;
        default:
            std::cout << "Get read" << std::endl;
            if (pfd.revents & POLLIN)
            {
                char buffer[128];
                ssize_t s = read(0, buffer, sizeof(buffer) - 1);
                if (s > 0)
                {
                    buffer[s] = 0;
                    write(1, buffer, strlen(buffer));
                }
                else if (s == 0)
                {
                    std::cout << "quit" << std::endl;
                }
                else
                {
                    std::cout << "read errno" << std::endl;
                }
            }
            break;
        }
    }
    return 0;
}